<template>
  <div class="order-contain">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-input
          placeholder="请输入内容"
          v-model="queryInfo.query"
          class="iptSearch"
          clearable
          @clear="getOrderList"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getOrderList"
          ></el-button>
        </el-input>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
          width="250"
        ></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger"
              >未付款</el-tag
            >
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" width="250">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-outline"
              size="mini"
              @click="showProgressDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单列表对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="dialogVisibleEdit"
      @close="editFormReset"
    >
      <el-form
        :model="editForm"
        :rules="editFormRule"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="editForm.address1"
            :options="cityData"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editOrder">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="dialogVisibleProgress">
      <!-- 时间线区域 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleProgress = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleProgress = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入城市数据
import cityData from 'assets/js/citydata'
export default {
  created() {
    this.getOrderList()
  },
  data() {
    return {
      // 订单数据列表
      orderList: [],
      // 订单数据的请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      // 总数目
      total: 0,
      // 修改订单列表对话框是否显示
      dialogVisibleEdit: false,
      // 修改订单列表表单数据
      editForm: {
        address1: [],
        address2: ''
      },
      // 修改订单列表验证规则
      editFormRule: {
        address1: [{ required: true, message: '请选择地址', trigger: 'blur' }],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 城市数据
      cityData,
      // 物流进度对话框是否显示
      dialogVisibleProgress: false,
      // 物流的信息
      activities: [
        {
          content: '活动按期开始',
          timestamp: '2018-04-15'
        },
        {
          content: '通过审核',
          timestamp: '2018-04-13'
        },
        {
          content: '创建成功',
          timestamp: '2018-04-11'
        }
      ]
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      console.log(this.queryInfo)
      console.log(res.data)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表数据失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页显示的条数改变
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    // 监听当前页码的改变
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    // 监听修改订单列表对话框的显示
    showEditDialog() {
      this.dialogVisibleEdit = true
    },
    // 监听层级选择器的改变
    handleChange() {},
    // 监听对话框关闭表单的重置
    editFormReset() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改订单列表
    editOrder() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) {
          return this.$message.error('请输入完整信息')
        }
        this.dialogVisibleEdit = false
      })
    },
    // 监听物流进度对话框显示
    async showProgressDialog() {
      this.dialogVisibleProgress = true
      // const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      // console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>
.iptSearch {
  width: 40%;
}
.el-cascader {
  width: 100%;
}
</style>
